{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f728f8a-be2a-43e6-a617-a4bd29a740f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip3 install tensorbay==1.16.0 #--use-feature=2020-resolver\n",
    "#!pip3 install unzip\n",
    "#!pip3 install torch==1.7.1 \n",
    "#!pip install keras-ncp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fd9cd2c8-14ee-4b7f-b6e2-aa268926c1e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!unzip archive.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b15fd53c-df4d-428b-8ba8-435fc69fc63d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch.nn as nn\n",
    "import kerasncp as kncp\n",
    "from kerasncp.torch import LTCCell\n",
    "#import pytorch_lightning as pl\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "de28d400-dff4-4ddf-94bf-421b89017ffb",
   "metadata": {},
   "outputs": [],
   "source": [
    "class RNNSequence(nn.Module):\n",
    "    def __init__(\n",
    "        self,\n",
    "        rnn_cell,\n",
    "    ):\n",
    "        super(RNNSequence, self).__init__()\n",
    "        self.rnn_cell = rnn_cell\n",
    "\n",
    "    def forward(self, x):\n",
    "        device = x.device\n",
    "        batch_size = x.size(0)\n",
    "        seq_len = x.size(1)\n",
    "        hidden_state = torch.zeros(\n",
    "            (batch_size, self.rnn_cell.state_size), device=device\n",
    "        )\n",
    "        outputs = []\n",
    "        for t in range(seq_len):\n",
    "            inputs = x[:, t]\n",
    "            new_output, hidden_state = self.rnn_cell.forward(inputs, hidden_state)\n",
    "            outputs.append(new_output)\n",
    "        outputs = torch.stack(outputs, dim=1)  # return entire sequence\n",
    "        return outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fa9c0c3b-5d39-4ccf-9d54-63c18e51036e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "pb = pd.read_csv('1.csv')\n",
    "zhishu = list(set(pb['order_book_id']))\n",
    "dt = pb.set_index(['order_book_id','date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "412ad6e1-8583-45fe-87d5-80d3a30e2eda",
   "metadata": {},
   "outputs": [],
   "source": [
    "dt[dt.pb_ratio_lf<0] = None\n",
    "dt[dt.pb_ratio_lf>20] = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7f560325-ad7d-4817-8633-bdc30e48037f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "259\n"
     ]
    }
   ],
   "source": [
    "count = [ ]\n",
    "for i in zhishu :\n",
    "     if np.any(pd.isnull(dt.loc[i])) == False:\n",
    "        count.append(i)\n",
    "count.sort()\n",
    "print(len(count))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4bff93cb-7a88-4323-902e-c602511e4d93",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "pb = pd.read_csv('11.csv')\n",
    "zhishu = list(set(pb['order_book_id']))\n",
    "data = pb.set_index(['order_book_id','date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8871994d-31d6-4ec1-842f-dfe5409572bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "data[data.pb_ratio_lf<0] = None\n",
    "data[data.pb_ratio_lf>20] = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "22675670-ce51-4354-b6fa-5579bcf174f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = data.dropna()\n",
    "#data['pb_ratio_lf'] = data['pb_ratio_lf'].apply(lambda x : int(np.round(200*(x-0)/20))) \n",
    "rw = pd.read_csv('21.csv')\n",
    "rw = rw.set_index(['order_book_id','date'])\n",
    "tr = pd.read_csv('31.csv')\n",
    "tr = tr.set_index(['order_book_id','tradedate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "65c44583-0ef4-4789-997a-fe61d76698e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "#from sklearn.preprocessing import scale\n",
    "seed = 111\n",
    "import random\n",
    "torch.manual_seed(seed) # 为CPU设置随机种子\n",
    "torch.cuda.manual_seed(seed) # 为当前GPU设置随机种子\n",
    "torch.cuda.manual_seed_all(seed)  # if you are using multi-GPU，为所有GPU设置随机种子\n",
    "np.random.seed(seed)  # Numpy module.\n",
    "random.seed(seed)  # Python random module.\t\n",
    "torch.backends.cudnn.benchmark = False\n",
    "torch.backends.cudnn.deterministic = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f20a79a3-6c73-415f-b5f7-2f823f5a975b",
   "metadata": {},
   "outputs": [],
   "source": [
    "n_sample = 100\n",
    "eb = nn.Embedding(n_sample, 9)\n",
    "#ids = count[0:30]\n",
    "dg = pd.read_csv('41.csv')\n",
    "dg = dg.set_index(['order_book_id','date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "54b71961-f342-4218-a28d-a2238becd98e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#date = np.random.choice(dg.index.get_level_values('date').values, size=10, replace=False)\n",
    "length = 128\n",
    "np.random.seed(seed)\n",
    "rap = np.random.choice(np.arange(20,41), size=10, replace=False)\n",
    "loop  = np.random.choice(count, size=n_sample, replace=False)\n",
    "sample  = np.random.choice(loop, size=n_sample, replace=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f5bdb16f-4171-4bb4-992e-391bb4d101a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def reward(end_date,reward_date,cd):\n",
    "    en = rw.loc[cd].loc[end_date].close\n",
    "    re = rw.loc[cd].loc[reward_date].close\n",
    "    #print(100*(re-en)/en)\n",
    "    return 100*(re-en)/en\n",
    "def tget(end_date,reward_date,cd):\n",
    "    #print(end_date)\n",
    "    en = rw.loc[rw.index.get_level_values('date')  ==  end_date].loc[cd].reset_index('date').close\n",
    "    #print(en)\n",
    "    re = rw.loc[rw.index.get_level_values('date')  ==  reward_date].loc[cd].reset_index('date').close\n",
    "    #print(100*(re-en)/en)\n",
    "    return (re-en)/en \n",
    "def seqs(start, ids):\n",
    "    \n",
    "    df = np.zeros((n_sample ,2,length))\n",
    "    \n",
    "    for i in ids:\n",
    "        pbs = data.loc[i].loc[dg.loc['000300.XSHG'].index[ start : start + length]].pb_ratio_lf \n",
    "        df[list(loop).index(i)][0] = pbs #scale(pbs)\n",
    "        df[list(loop).index(i)][1]  = tr.loc[i].loc[dg.loc['000300.XSHG'].index[ start : start + length]].today\n",
    "    #df[list(loop).index(i)][1]  = scale( dg.loc['000300.XSHG'][ start : start + length].close )\n",
    "    df[:,0,:] = df[:,0,:] + np.random.randn(n_sample,length)\n",
    "    return np.array(df).transpose(2,1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "549188e5-d5d2-44fe-afcd-391d6768d6d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.00526316  0.04473684 -0.00789474  0.13684211  0.07105263 -0.02105263\n",
      " -0.04736842  0.08421053  0.11052632  0.01842105]\n"
     ]
    }
   ],
   "source": [
    "ct = 10\n",
    "lp = np.random.choice(np.linspace(-0.1,0.15,num = 20), size=ct, replace=False)\n",
    "print(lp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dc6a338e-f67e-41e3-92d2-46c4574b63aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "from copy import deepcopy\n",
    "from torch import Tensor\n",
    "from torch.nn.modules import Module\n",
    "from typing import Optional, Tuple, Union\n",
    "\n",
    "#from . import Hopfield\n",
    "import sys\n",
    "#sys.path.append(\"../input/myhopfieldttt1/hopfield-layers/modules\")\n",
    "sys.path.append(\"hopfield-layers/modules\")\n",
    "from mytest import Hopfield,HopfieldPooling\n",
    "#from torch.nn import GELU\n",
    "####\n",
    "class HopfieldEncoderLayer(Module):\n",
    "    \"\"\"\n",
    "    Module with underlying Hopfield association to be used as an encoder in transformer-like architectures.\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self,\n",
    "                 hopfield_association: Hopfield,\n",
    "                 dim_feedforward: int = 2048,\n",
    "                 dropout: float = 0.1\n",
    "                 #activation:torch.nn.GELU()\n",
    "                 ):\n",
    "        \"\"\"\n",
    "        Initialise a new instance of a Hopfield association-based encoder module.\n",
    "\n",
    "        :param hopfield_association: instance of Hopfield association module\n",
    "        :param dim_feedforward: depth of the linear projections applied internally\n",
    "        :param activation: activation to be applied on the result of the internal linear projections\n",
    "        :param dropout: dropout probability to be applied internally\n",
    "        \"\"\"\n",
    "        super(HopfieldEncoderLayer, self).__init__()\n",
    "        self.hopfield_association = deepcopy(hopfield_association)\n",
    "\n",
    "        self.linear_residual = nn.Linear(self.hopfield_association.state_pattern_dim, dim_feedforward)\n",
    "        self.dropout_residual = nn.Dropout(dropout)\n",
    "        self.linear_output = nn.Linear(dim_feedforward, self.hopfield_association.state_pattern_dim)\n",
    "\n",
    "        self.norm_residual = nn.LayerNorm(self.hopfield_association.state_pattern_dim)\n",
    "        self.norm_output = nn.LayerNorm(self.hopfield_association.state_pattern_dim)\n",
    "        self.dropout_hopfield_association = nn.Dropout(dropout)\n",
    "        self.dropout_output = nn.Dropout(dropout)\n",
    "\n",
    "        self.activation_residual =  torch.nn.GELU()\n",
    "        #self.activation_residual =  Sine()\n",
    "        #assert self.activation_residual is not None, r'invalid activation function supplied.'\n",
    "\n",
    "    def forward(self, src: Tensor, src_mask: Optional[Tensor] = None,\n",
    "                src_key_padding_mask: Optional[Tensor] = None) -> Tensor:\n",
    "        \"\"\"\n",
    "        Apply Hopfield encoding on specified data.\n",
    "\n",
    "        :param src: data to be processed by Hopfield encoder module\n",
    "        :param src_mask: mask to be applied on association matrix\n",
    "        :param src_key_padding_mask: mask to be applied on stored patterns\n",
    "        :return: Hopfield-encoded input data\n",
    "        \"\"\"\n",
    "        data_associated = self.hopfield_association(\n",
    "            input=src, stored_pattern_padding_mask=src_key_padding_mask, association_mask=src_mask)\n",
    "        src = src + self.dropout_hopfield_association(input=data_associated)\n",
    "        src = self.norm_residual(input=src)\n",
    "\n",
    "        result_residual_inner = self.activation_residual(self.linear_residual(input=src))\n",
    "        data_associated = self.linear_output(input=self.dropout_residual(input=result_residual_inner))\n",
    "        src = src + self.dropout_output(input=data_associated)\n",
    "\n",
    "        return self.norm_output(input=src)\n",
    "\n",
    "    def get_association_matrix(self, input: Union[Tensor, Tuple[Tensor, Tensor, Tensor]]) -> Tensor:\n",
    "        \"\"\"\n",
    "        Fetch Hopfield association matrix gathered by passing through the specified data.\n",
    "\n",
    "        :param input: data to be passed through the Hopfield association\n",
    "        :return: association matrix as computed by the Hopfield core module\n",
    "        \"\"\"\n",
    "        return self.hopfield_association.get_association_matrix(input=input)\n",
    "\n",
    "    @property\n",
    "    def batch_first(self) -> int:\n",
    "        return self.hopfield_association.batch_first\n",
    "\n",
    "    @property\n",
    "    def input_size(self) -> int:\n",
    "        return self.hopfield_association.input_size\n",
    "\n",
    "    @property\n",
    "    def output_size(self) -> int:\n",
    "        return self.linear_output.out_features\n",
    "\n",
    "\n",
    "class HopfieldDecoderLayer(Module):\n",
    "\n",
    "    def __init__(self,\n",
    "                 hopfield_association_self: Hopfield,\n",
    "                 hopfield_association_cross: Hopfield,\n",
    "                 dim_feedforward: int = 2048,\n",
    "                 dropout: float = 0.1\n",
    "                 #activation: str = r'relu'\n",
    "                 ):\n",
    "        \"\"\"\n",
    "        Initialise a new instance of a Hopfield association-based encoder module.\n",
    "\n",
    "        :param hopfield_association_self: instance of Hopfield self-association module\n",
    "        :param hopfield_association_cross: instance of Hopfield cross-association module\n",
    "        :param dim_feedforward: depth of the linear projections applied internally\n",
    "        :param dropout: dropout probability to be applied internally\n",
    "        :param activation: activation to be applied on the result of the internal linear projections\n",
    "        \"\"\"\n",
    "        super(HopfieldDecoderLayer, self).__init__()\n",
    "        self.hopfield_association_self = deepcopy(hopfield_association_self)\n",
    "        self.hopfield_association_cross = deepcopy(hopfield_association_cross)\n",
    "\n",
    "        self.linear_residual = nn.Linear(self.hopfield_association_self.state_pattern_dim, dim_feedforward)\n",
    "        self.dropout_residual = nn.Dropout(dropout)\n",
    "        self.linear_output = nn.Linear(dim_feedforward, self.hopfield_association_self.state_pattern_dim)\n",
    "\n",
    "        self.norm_residual_self = nn.LayerNorm(self.hopfield_association_self.state_pattern_dim)\n",
    "        self.norm_residual_cross = nn.LayerNorm(self.hopfield_association_self.state_pattern_dim)\n",
    "        self.norm_output = nn.LayerNorm(self.hopfield_association_self.state_pattern_dim)\n",
    "        self.dropout_hopfield_association_self = nn.Dropout(dropout)\n",
    "        self.dropout_hopfield_association_cross = nn.Dropout(dropout)\n",
    "        self.dropout_output = nn.Dropout(dropout)\n",
    "\n",
    "        self.activation_residual = torch.nn.GELU()\n",
    "        #self.activation_residual =  Sine()\n",
    "        #assert self.activation_residual is not None, r'invalid activation function supplied.'\n",
    "\n",
    "    def forward(self, tgt: Tensor, memory: Tensor, tgt_mask: Optional[Tensor] = None,\n",
    "                memory_mask: Optional[Tensor] = None, tgt_key_padding_mask: Optional[Tensor] = None,\n",
    "                memory_key_padding_mask: Optional[Tensor] = None) -> Tensor:\n",
    "        \"\"\"\n",
    "        Apply Hopfield decoding on specified data.\n",
    "\n",
    "        :param tgt: data to be processed by Hopfield decoder module (self-association)\n",
    "        :param memory: data to be processed by Hopfield encoder module (cross-association)\n",
    "        :param tgt_mask: mask to be applied on self-association matrix\n",
    "        :param memory_mask: mask to be applied on cross-association matrix\n",
    "        :param tgt_key_padding_mask: mask to be applied on stored patterns\n",
    "        :param memory_key_padding_mask: mask to be applied on state patterns as well as pattern projection\n",
    "        :return: Hopfield-decoded input\n",
    "        \"\"\"\n",
    "        data_associated = self.hopfield_association_self(\n",
    "            input=tgt, stored_pattern_padding_mask=tgt_key_padding_mask,\n",
    "            association_mask=tgt_mask)\n",
    "        tgt = tgt + self.dropout_hopfield_association_self(input=data_associated)\n",
    "        tgt = self.norm_residual_self(input=tgt)\n",
    "\n",
    "        data_associated = self.hopfield_association_cross(\n",
    "            input=(memory, tgt, memory), stored_pattern_padding_mask=memory_key_padding_mask,\n",
    "            association_mask=memory_mask)\n",
    "        tgt = tgt + self.dropout_hopfield_association_cross(input=data_associated)\n",
    "        tgt = self.norm_residual_cross(input=tgt)\n",
    "\n",
    "        result_residual_inner = self.activation_residual(self.linear_residual(input=tgt))\n",
    "        data_associated = self.linear_output(input=self.dropout_residual(input=result_residual_inner))\n",
    "        tgt = tgt + self.dropout_output(input=data_associated)\n",
    "        return self.norm_output(input=tgt)\n",
    "\n",
    "    def get_association_matrix_self(self, input: Union[Tensor, Tuple[Tensor, Tensor, Tensor]]) -> Tensor:\n",
    "        \"\"\"\n",
    "        Fetch Hopfield self-association matrix gathered by passing through the specified data.\n",
    "\n",
    "        :param input: data to be passed through the Hopfield association\n",
    "        :return: association matrix as computed by the Hopfield core module\n",
    "        \"\"\"\n",
    "        return self.hopfield_association_self.get_association_matrix(input=input)\n",
    "\n",
    "    def get_association_matrix_cross(self, input: Union[Tensor, Tuple[Tensor, Tensor, Tensor]]) -> Tensor:\n",
    "        \"\"\"\n",
    "        Fetch Hopfield cross-association matrix gathered by passing through the specified data.\n",
    "\n",
    "        :param input: data to be passed through the Hopfield association\n",
    "        :return: association matrix as computed by the Hopfield core module\n",
    "        \"\"\"\n",
    "        return self.hopfield_association_cross.get_association_matrix(input=input)\n",
    "\n",
    "    @property\n",
    "    def batch_first(self) -> int:\n",
    "        return self.hopfield_association_self.batch_first\n",
    "\n",
    "    @property\n",
    "    def input_size(self) -> int:\n",
    "        return self.hopfield_association_self.input_size\n",
    "\n",
    "    @property\n",
    "    def output_size(self) -> int:\n",
    "        return self.linear_output_self.out_features\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e060a3a2-46be-40b3-8642-33b59467d65d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alloc!\n"
     ]
    }
   ],
   "source": [
    "in_features = 2*n_sample\n",
    "out_features = 2*n_sample\n",
    "wiring = kncp.wirings.FullyConnected(16, out_features)  # 16 units, 8 motor neurons\n",
    "ltc_cell = LTCCell(wiring, in_features)\n",
    "ltc_sequence = RNNSequence(\n",
    "    ltc_cell,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "07681768-fb14-4f25-946c-d24955e23cc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create your own Encoder\n",
    "class DEcoder(nn.Module):\n",
    "        \n",
    "    def __init__(self,):\n",
    "        super(DEcoder, self).__init__()\n",
    "        self.dence = nn.Linear(2, 1000)\n",
    "        #2*n_sample*length\n",
    "        self.dence1 = nn.Linear(out_features*length, 50)\n",
    "        self.dence2 = nn.Linear(1000, 1000)\n",
    "        self.fc = nn.Linear(1050, 2*n_sample)\n",
    "        self.sig = nn.Sigmoid()\n",
    "        #encoder = HopfieldEncoderLayer(Hopfield(203*203))\n",
    "        self.encoder1 = HopfieldEncoderLayer(Hopfield(2*n_sample,num_heads=4))\n",
    "        self.encoder2 = HopfieldEncoderLayer(Hopfield(2*n_sample,num_heads=4))#,dim_feedforward = 20\n",
    "        \n",
    "        self.relu = nn.LeakyReLU(negative_slope=0.3, inplace=True)\n",
    "        self.norm_output = nn.LayerNorm(32)\n",
    "        self.hop = HopfieldPooling(512)\n",
    "        self.ltc = ltc_sequence\n",
    "    def forward(self,x,x1):\n",
    "        #print(x.shape)\n",
    "        out = self.encoder1(x)\n",
    "        out = self.encoder2(out)\n",
    "        out1 = x1\n",
    "        #print(out1.shape)\n",
    "        out = out.view(-1, out_features*length)   \n",
    "        out = self.dence1(out)\n",
    "        #print(out.shape)\n",
    "        out2 = torch.cat([out,out1],1)\n",
    "        out2 = self.fc(out2)\n",
    "        #out2 = self.sig(out2)\n",
    "        return out2\n",
    "# create your own D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9b9df136-8dc4-483d-a6ab-6583ed1ecb34",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = DEcoder()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8132daba-68f9-4360-8bdf-0deaeff6ac76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.load_state_dict(torch.load('models.weights'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6b4bce1a-e86c-4f6d-8433-1fe5ac375ac7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#torch.save(model.state_dict(), 'models.weights')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "be8a0b29-bdb5-4cfd-9c02-a2c72b535a39",
   "metadata": {},
   "outputs": [],
   "source": [
    "#model.load_state_dict(torch.load('model.weights'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fdca246e-b7d7-424e-a8ab-0ea2a11d9d03",
   "metadata": {},
   "outputs": [],
   "source": [
    "rand = np.load('rand.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f41e038f-be38-4e26-b238-15710098c0ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import random\n",
    "import datetime\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "da461bea-287d-41d7-bd00-690645061bdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "date='2021-11-26'\n",
    "\n",
    "#date = datetime.datetime.strptime(date, \"%Y-%m-%d\")\n",
    "\n",
    "start = list(dg.index.get_level_values('date').values).index(date)-length + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fe7da9f9-262f-4018-b57c-ecfa98a2d777",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-10-29\n"
     ]
    }
   ],
   "source": [
    "pic = []\n",
    "#targets = []\n",
    "val_pic = []\n",
    "val_tag = []\n",
    "\n",
    "\n",
    "k = 20        \n",
    "sq =seqs(start,sample) \n",
    "ex_start = list(dg.index.get_level_values('date').values).index(date)- k - length + 1\n",
    "ex_end = list(dg.index.get_level_values('date').values).index(date)- k\n",
    "                          \n",
    "ex_end_date  =  dg.loc['000300.XSHG'].index[ex_end]\n",
    "print(ex_end_date)   \n",
    "ex_reward_date = date\n",
    "    \n",
    "#print(date) \n",
    "ex_sq = seqs(ex_start,sample)\n",
    "    \n",
    "pic.append(sq)\n",
    "        \n",
    "# print(dg.loc['000300.XSHG'].index[ex_start],ex_end_date,ex_reward_date)                 \n",
    "val_pic.append(ex_sq)\n",
    "        \n",
    "val_tag.append(tget(ex_end_date,ex_reward_date,sample))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6f7952e2-082d-4e84-abb9-444cc532455d",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = model.cuda()\n",
    "model.eval()\n",
    "#.transpose(0,3,2,1)\n",
    "pps = np.array(pic)\n",
    "logits = model(torch.tensor(pps).cuda().reshape(-1,length,n_sample*2).float(),torch.tensor(np.array(rand[3:4])).cuda().float())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f8c3ab88-6f4a-4a24-a0a3-c97dce4eb92b",
   "metadata": {},
   "outputs": [],
   "source": [
    "out = nn.Softmax(dim=1)(logits.view(-1, 2,n_sample))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0d1e7aa3-b4aa-4c10-9713-1064003c47bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(ex_out.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "902d44bf-bd94-484a-b832-fc20f0140e60",
   "metadata": {},
   "outputs": [],
   "source": [
    "ex_logits = model(torch.tensor(np.array(val_pic)).cuda().reshape(-1,length,n_sample*2).float(),torch.tensor(np.array(rand[3:4])).cuda().float())\n",
    "ex_out = nn.Softmax(dim=1)(ex_logits.view(-1, 2,n_sample))\n",
    "#ex_ex = np.multiply(ex.T,ex_out[:,1,:].detach().cpu().numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "09014f26-b7a7-486d-ab36-54e2b9c10130",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds =pd.DataFrame()\n",
    "#ds['value']  = ex_ex_qujian.mean(axis=0)\n",
    "ds[ 'tag'] = np.array(val_tag)[0]\n",
    "ds['m_max'] = ex_out[0,1,:].detach().cpu().numpy()\n",
    "ds['code']   = sample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b2ebf1b4-a464-472e-9bda-bb0680bd13f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         tag     m_max         code\n",
      "43 -0.004587  0.000140  000008.XSHE\n",
      "42 -0.021956  0.000145  601336.XSHG\n",
      "21 -0.002799  0.000157  600109.XSHG\n",
      "92  0.006891  0.000164  000423.XSHE\n",
      "19 -0.039653  0.000165  600585.XSHG\n",
      "86  0.025084  0.000168  000559.XSHE\n",
      "65 -0.003521  0.000183  002310.XSHE\n",
      "94  0.062500  0.000188  002465.XSHE\n",
      "66  0.159091  0.000190  002456.XSHE\n",
      "97  0.101351  0.000195  600372.XSHG\n",
      "6   0.025017  0.000214  000876.XSHE\n",
      "27 -0.041473  0.000221  000651.XSHE\n",
      "9  -0.029293  0.000228  002007.XSHE\n",
      "50 -0.065789  0.000246  600660.XSHG\n",
      "69  0.000582  0.000286  000333.XSHE\n",
      "11  0.036603  0.000301  002797.XSHE\n",
      "55 -0.098462  0.000318  000001.XSHE\n",
      "25  0.153352  0.000392  600406.XSHG\n",
      "39 -0.018293  0.000434  600029.XSHG\n",
      "75  0.009639  0.000437  600352.XSHG\n",
      "90  0.115810  0.000484  600111.XSHG\n",
      "85 -0.016367  0.000488  000425.XSHE\n",
      "79  0.008587  0.000517  600362.XSHG\n",
      "34 -0.016694  0.000528  603993.XSHG\n",
      "16 -0.022491  0.000535  002294.XSHE\n",
      "47 -0.051383  0.000592  002470.XSHE\n",
      "49 -0.187621  0.000604  002411.XSHE\n",
      "87  0.072714  0.000791  000738.XSHE\n",
      "73  0.000000  0.001035  000725.XSHE\n",
      "77  0.019802  0.001051  000100.XSHE\n",
      "64  0.002434  0.001149  002555.XSHE\n",
      "5  -0.109231  0.001161  000625.XSHE\n",
      "78  0.137221  0.001406  600804.XSHG\n",
      "81  0.269375  0.001568  002174.XSHE\n",
      "38  0.030558  0.001658  600188.XSHG\n",
      "1   0.055623  0.001933  300059.XSHE\n",
      "51  0.732938  0.002621  600522.XSHG\n",
      "56  0.261824  0.003863  002153.XSHE\n",
      "7  -0.069596  0.007574  000963.XSHE\n",
      "40 -0.105845  0.011184  601012.XSHG\n"
     ]
    }
   ],
   "source": [
    "print(ds.sort_values('m_max')[-40:])\n",
    "#print(ds.sort_values('tag')[-40:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2e4935bf-f68b-4621-bac5-84d8282359d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(ex_out[:,1,:].shape)\n",
    "#cu = 0\n",
    "#print(ds.loc[cu].code)\n",
    "#plt.plot(ex_out[0,1,:].detach().cpu().numpy()[:,cu])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b80f4fd5-e21c-479a-81cb-9566c0119214",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df4f4238-a090-4158-8f6a-6eee7bb59617",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "default:Python",
   "language": "python",
   "name": "conda-env-default-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
